package com.lmonkey.filter;

import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@MultipartConfig
@WebFilter("/admin/admin_doproductadd")
public class DoProductAdd implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
          HttpServletRequest reqq = (HttpServletRequest)req;
          HttpServletResponse respp = (HttpServletResponse)resp;
        //设置字符集
        reqq.setCharacterEncoding("UTF-8");
        respp.setContentType("text/html;charset=utf-8");
        PrintWriter out = respp.getWriter();
        //获取参数
        String pname = reqq.getParameter("productname");
        if (pname.equals("")){
            out.println("<script>alert('商品名称不能为空');location.href='admin_toproductadd';</script>");
            out.close();
            return;
        }
        //获取part对象
        Part part = reqq.getPart("photo");
        String filename = part.getSubmittedFileName();
        if (filename == ""){
            out.println("<script>alert('商品图片必须上传');location.href='admin_toproductadd';</script>");
            out.close();
            return;
        }else{
            Pattern pt = Pattern.compile(".+(.gif|.jpg|.jpeg|.png|.GIF|.JPG|.PNG)$");
            Matcher mc = pt.matcher(filename);
            if (!mc.matches()){
                out.println("<script>alert('图片文件格式不正确');location.href='admin_toproductadd';</script>");
                out.close();
                return;
            }
        }

        String price =reqq.getParameter("productprice");
        if (price.equals("")){
            out.println("<script>alert('商品价格不能为空');location.href='admin_toproductadd';</script>");
            out.close();
            return;
        }else {
            Pattern pt = Pattern.compile("^[\\d+\\.\\d+]{1,15}$");
            Matcher mc = pt.matcher(price);
            if (!mc.matches()){
                out.println("<script>alert('只能输入数字');location.href='admin_toproductadd';</script>");
                out.close();
                return;
            }
        }

        String desc = reqq.getParameter("productDesc");
        if (desc.equals("")){
            out.println("<script>alert('商品介绍必须填写');location.href='admin_toproductadd';</script>");
            out.close();
            return;
        }

        String stock =reqq.getParameter("productstock");
        if (stock.equals("")){
            out.println("<script>alert('商品库存必须填写');location.href='admin_toproductadd';</script>");
            out.close();
            return;
        }else {
            Pattern pt = Pattern.compile("^[0-9]{1,10}$");
            Matcher mc = pt.matcher(stock);
            if (!mc.matches()){
                out.println("<script>alert('只能填写整数');location.href='admin_toproductadd';</script>");
                out.close();
                return;
            }
        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
